<?php
namespace app\a\controller;
use think\facade\Db;
class News extends Base{

    //列表数据
    public function list_data(){
        $param = input('');

        $search_type = $param['type'] ? ['type_id' => $param['type']]:[];//判断是否需要筛选 分类
        $search = $param['keyWord'] ? [['title', 'like', '%'.$param['keyWord'].'%']]:[];//判断是否需要筛选 关键词
        $search_sort = $param['prop'] ? [$param['prop'] => $param['sort']]:[];//排序
        $listData = Db::table('an_news')
        // ->fetchSql(true)//输出mysql语句
        ->page($param['page'],$param['limit'])
        ->order($search_sort)
        ->where($search)
        ->where($search_type)
        ->withoutField(['content'])
        ->select()
        ->toArray(); 
        $this->info['data'] = [
            'list' => $listData,
            'type' => Db::table('an_type')->where('type', 'news')->order('id', 'desc')->select()->toArray()
        ];
        $this->info['total'] = Db::table('an_news')->where($search_type)->where($search)->count();
        return json($this->info);
    }

    //获取需要编辑数据
    public function get_detail_a(){
        $param = input('');
        $details = Db::table('an_news')->where('id',$param['id'])->find();
        $this->info['data'] = [
            'details'=>$details,
            'type' => []
        ];
        return json($this->info);
    }

    //删除数据
    public function del_data_a(){
        $param = input('');
        foreach($param['ids'] as $v){
            Db::table('an_news')->where('id',$v)->delete();
        }
        $this->logData('删除文章，id为'.implode(",", $param['ids']),$param['token']);
        return json($this->info);
    }
    

    //更新数据
    public function updata_a(){
        $param = input('');
        Db::table('an_news')->where('id',$param['id'])->update([
            'up_time'=>time(),
            'title'=>$param['title'],
            'min_title'=>$param['min_title'],
            'cover'=>$param['cover'],
            'url_name'=>$param['url_name'],
            'url_show'=>$param['url_show'],
            'url_type'=>$param['url_type'],
            'url'=>$param['url'],
            'share_title'=>$param['share_title'],
            'share_img'=>$param['share_img'],
            'content'=>$param['content'],
            'sort'=>isset($param['sort'])?$param['sort']:0,
            'keywords'=>$param['keywords'],
            'description'=>$param['description'],
            'video_url'=>$param['video_url'],
            'addr'=>$param['addr'],
            "a_show"=>$param['a_show'],
            "type_id"=>$param['type_id'],
            "abstract"=>$param['abstract'],
        ]);
        $this->info['msg']='更新成功';
        $this->logData('更新资讯，id为'.$param['id'],$param['token']);
        return json($this->info);
    } 

    //新增数据
    public function add_data_a(){
        $param = input('');
        Db::table('an_news')->insert([
            'create_time'=>time(),
            'up_time'=>time(),
            'title'=>$param['title'],
            'min_title'=>$param['min_title'],
            'cover'=>$param['cover'],
            'url_name'=>$param['url_name'],
            'url_show'=>$param['url_show'],
            'url_type'=>$param['url_type'],
            'url'=>$param['url'],
            'share_title'=>$param['share_title'],
            'share_img'=>$param['share_img'],
            'content'=>$param['content'],
            'sort'=>isset($param['sort'])?$param['sort']:0,
            'keywords'=>$param['keywords'],
            'description'=>$param['description'],
            'video_url'=>$param['video_url'],
            'addr'=>$param['addr'],
            "a_show"=>$param['a_show'],
            "type_id"=>$param['type_id'],
            "abstract"=>$param['abstract'],
        ]);
        $this->info['msg']='新增成功';
        $this->logData('新增资讯，名称为'.$param['title'],$param['token']);
        return json($this->info);
    }  

}
